<?php

namespace app\planning\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Exception;
use think\cache\driver\Redis;
use app\planning\controller\Index;
use Swoole\Coroutine as co;

class Planning extends Command {

	protected $server;
	protected $indexClass;
	protected $redis;

	// 命令行配置函数
	protected function configure() {
		// setName 设置命令行名称 && setDescription 设置命令行描述
		$this->setName('ubaby:server')->setDescription('Start ubaby Server!');
	}

	// 设置命令返回信息
	protected function execute(Input $input, Output $output) {
		$this->server = new \swoole_http_server("0.0.0.0", 9888);
		$this->server->set(array(
			'worker_num' => 2, //工作进程数量
			'daemonize' => true, //是否作为守护进程
			'task_worker_num' => 10,
			'max_request' => 50,
			'user' => 'nginx',
			'group' => 'program',
			'log_file' => ROOT_PATH . 'data/mjh_swoole.log',
		));
		$this->server->on('Start', [$this, 'onStart']);
		$this->server->on('WorkerStart', [$this, 'onWorkerStart']);
		$this->server->on('Request', [$this, 'onRequest']);
		$this->server->on('Task', [$this, 'onTask']);
		$this->server->on('Finish', [$this, 'onFinish']);

		$this->server->start();
	}

	public function onStart(\swoole_server $serv) {
		cli_set_process_title("ubaby_server");
	}

	// Worker/Task进程启动时回调函数
	public function onWorkerStart(\swoole_server $serv, $worker_id) {
		$this->redis = new Redis();
		$this->indexClass = new Index($this->server, $this->redis);

		if (0 == $worker_id) {

			//热重启进程
			//  require_once "/home/www/src/dev/code/anjiema/public/reload_page.php";
			// Test();
			// 启动 Timer 定时器,每5分钟回调一次 asyncWriteDatabase 函数
			swoole_timer_tick(600000, [$this->indexClass, 'workersExam']); //员工考核
			swoole_timer_tick(1000, [$this->indexClass, 'userUnbind']); //客户解绑
			swoole_timer_tick(1000, [$this->indexClass, 'followWarn']); //跟进提醒,待测试
			swoole_timer_tick(1000, [$this->indexClass, 'userUnbindWarn']); //客户自动解绑提醒
		}
	}

	// 响应函数
	public function onRequest(\swoole_http_request $request, \swoole_http_response $response) {

		try {
			$data = $request->post;
			/* if(!empty($data) && isset($data['order_id'])){
			  // 投递异步任务，使用异步完成订单记录写入 Redis 队列
			  $this->server->task(json_encode($data));
			  } */
		} catch (Exception $exception) {
			$response->end(json_encode(['code' => $exception->getCode(), 'message' => $exception->getMessage()]));
		}
	}

	// 异步任务处理函数
	public function onTask(\swoole_server $serv, $task_id, $src_worker_id, $data) {
		echo 'task';
		$data = json_decode($data, true);
		if ($data['type'] == 1) {  //员工考核
			$this->indexClass->workersExamTask($data);
		} else if ($data['type'] == 2) {//自动解绑客户
			$this->indexClass->userUnbindTask($data);
		} else if ($data['type'] == 3) {//跟进提醒
			$this->indexClass->followWarnTask($data);
		}else if ($data['type'] == 4) {//自动解绑客户提醒
			$this->indexClass->userUnbindWarnTask($data);
		}
		
		return true;
	}

	// 异步任务完成通知
	public function onFinish(\swoole_server $server, $task_id, $data) {

		echo "完成";
	}

}
